home *** CD-ROM | disk | FTP | other *** search
- /**************************************************
- *
- * Sample Movie Player 3
- *
- * This program demonstrates how to open a movie file
- * and play it in a window using the Movie Controller Thing
- *
- * Sample.h has definitions and equates
- * Main.c contains the general Mac application stuff
- * Movie Stuff.c has most of the interesting movie code
- *
- * Interesting things to look at:
- * How to open and close movies
- * Finding the and getting the Movie Controller Thing
- * Calling the player thing from the event loop
- * Not having to do much else since the Movie Controller does it all for you
- *
- * Rich Williams 11/90, 2/91
- * Updated to 4.04 interfaces
- * Updated to MPW/Think 5.0/DSG interfaces 9/91 Chris Thorman
- *
- ***************************************************/
-
- #include "Movies.h"
- #include "Mini Player3.h"
-
- #include <Memory.h>
- #include <Fonts.h>
- #include <Palettes.h>
- #include <OSEvents.h>
- #include <Menus.h>
- #include <Sound.h>
- #include <ToolUtils.h>
- #include <Desk.h>
- #include <Resources.h>
-
- /**************************************************
- *
- * General Info
- *
- ***************************************************/
-
- extern numOpenMovies;
-
- /**************************************************
- *
- * various globals
- *
- ***************************************************/
-
- extern int theModifiers; /* Used to keep the event modifiers */
- Rect dragRect;
- MenuHandle appleMenu, fileMenu, editMenu;
-
- MovieInstance movieList[maxMovies]; /* Array of open movies */
-
-
- /**************************************************
- ***************************************************
- *
- * Initialization Routines
- *
- * All of the stuff we call to get things started
- *
- ***************************************************
- ***************************************************/
-
- /**************************************************
- *
- * InitMacintosh() Initialize all the managers & memory
- *
- ***************************************************/
- void InitMacintosh()
-
- {
- MaxApplZone();
- InitGraf(&qd.thePort);
- InitPalettes();
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
-
- }
-
- /**************************************************
- *
- * SetUpMenus()
- *
- * Reads in the menu resources and installs them
- *
- *
- ***************************************************/
- void SetUpMenus()
-
- {
- InsertMenu(appleMenu = GetMenu(appleID), 0);
- InsertMenu(fileMenu = GetMenu(fileID), 0);
- InsertMenu(editMenu = GetMenu(editID), 0);
- DrawMenuBar();
- AddResMenu(appleMenu, 'DRVR');
-
- }
-
- /**************************************************
- *
- * SetUpWindows()
- *
- * INits stuff for windows
- *
- ***************************************************/
- void SetUpWindows()
-
- {
- /* Create the window for the picture */
- dragRect = qd.screenBits.bounds;
-
- }
-
-
- /**************************************************
- ***************************************************
- *
- * Routines for handling menus.
- *
- ***************************************************
- ***************************************************/
-
- /**************************************************
- *
- * AdjustMenus()
- *
- * Enable or disable the items in the Edit menu if a DA window
- * comes up or goes away. Our application doesn't do anything with
- * the Edit menu.
- *
- ***************************************************/
- void AdjustMenus()
- {
- register WindowPeek wp = (WindowPeek) FrontWindow();
- short kind = wp ? wp->windowKind : 0;
- Boolean DA = kind < 0;
-
- enable(editMenu, 1, DA);
- enable(editMenu, 3, DA);
- enable(editMenu, 4, DA);
- enable(editMenu, 5, DA);
- enable(editMenu, 6, DA);
-
- enable(fileMenu, openItem, numOpenMovies<maxMovies);
- enable(fileMenu, closeItem, DA || (numOpenMovies > 0));
-
- }
-
- static
- void enable(MenuHandle menu, int item, Boolean ok)
- {
- if (ok)
- EnableItem(menu, item);
- else
- DisableItem(menu, item);
- }
-
- /**************************************************
- *
- * HandleMenu (mSelect)
- *
- * Handle the menu selection. mSelect is what MenuSelect() and
- * MenuKey() return: the high word is the menu ID, the low word
- * is the menu item
- *
- ***************************************************/
- void HandleMenu (mSelect)
-
- long mSelect;
-
- {
- int menuID = HiWord(mSelect);
- int menuItem = LoWord(mSelect);
- Str255 name;
- GrafPtr savePort;
- WindowPeek frontWindow;
-
- switch (menuID)
- {
- case appleID: /* The Apple menu */
- switch (menuItem)
- {
- case aboutItem:
- DoAboutBox(); /* Display the About Box */
- break;
-
- default: /* It's a DA */
- GetPort(&savePort);
- GetItem(appleMenu, menuItem, name);
- OpenDeskAcc(name);
- SetPort(savePort);
- break;
- }
- break;
-
- case fileID: /* The file menu */
- switch (menuItem)
- {
- case openItem:
- DoOpen(); /* Open a movie */
- break;
-
- case closeItem:
- if ((frontWindow = (WindowPeek) FrontWindow()) == 0L)
- break;
-
- if (frontWindow->windowKind < 0)
- CloseDeskAcc(frontWindow->windowKind);
- else
- /* It's the movie window ( the only one we've got ) */
- /* Close it & clean up the movie stuff */
- CloseEm( (WindowPtr) frontWindow);
-
- break;
-
-
- case quitItem: /* Pack up and go home */
- CloseAllWindows();
- ExitToShell();
- break;
- }
- break;
-
- case editID:
- if (!SystemEdit(menuItem-1))
- SysBeep(5);
- break;
- }
- }
-
- /**************************************************
- *
- * CloseEm(w:WindowPtr) Closes window w
- * if w is a movie window, clean up the movie and the player
- *
- ***************************************************/
- void CloseEm(w)
-
- WindowPtr w;
-
- {
- MovieInstance *theMovie;
-
- HideWindow(w);
- theMovie = WhichMovieWindow(w); /* Is it a movie window */
- if (theMovie) /* If its the movie window, throw out the movie */
- {
- CleanUpMovie(theMovie);
- }
- }
-
-
- /**************************************************
- *
- * CloseAllWindows()
- * Throws out all windows & movies
- *
- ***************************************************/
- void CloseAllWindows()
-
- {
- short movieCount;
-
- /* Throw out all of the movies */
- for (movieCount = 0;movieCount<maxMovies;movieCount++)
- CleanUpMovie(&movieList[movieCount]);
-
- /* Put any other windows here */
- }
-
- /**************************************************
- *
- * DoAboutBox() Draws and displays the about box
- *
- ***************************************************/
- void DoAboutBox()
-
- {
- DialogPtr theDialog; /* Stuff for the about box */
- short itemHit, io;
- Handle tempHandle, soundHandle;
- Rect box;
-
- soundHandle = GetResource('snd ',meowID);
- theDialog = GetNewDialog(aboutDlgID,0L,(WindowPtr)-1L);
-
- /* Highlight the default button */
- SetPort(theDialog);
- GetDItem(theDialog,1,&itemHit,&tempHandle,&box);
- PenSize(3,3);
- InsetRect(&box,-4,-4);
- FrameRoundRect(&box,16,16);
-
- /* Wait until the button is pressed */
- do
- ModalDialog(0l,&itemHit);
- while (itemHit != 1);
-
- /* Button pressed, now meow and go home */
- io = SndPlay(0L,soundHandle,0);
- ReleaseResource(soundHandle);
- DisposDialog(theDialog);
- }
-
- /**************************************************
- *
- * DoOpen()
- *
- * Brings up standard file dialog
- * and opens the movie if one has been selected
- *
- ***************************************************/
- void DoOpen()
-
- {
- StandardFileReply sfr; /* New-style SF reply */
- SFTypeList myTypeList = {'MooV'};
-
- StandardGetFilePreview(0, 2, myTypeList, &sfr);
- if (sfr.sfGood)
- OpenTheMovie(&sfr);
-
- }
-
- /**************************************************
- ***************************************************
- *
- * The window routines
- *
- ****************************************************
- ***************************************************/
-
- /**************************************************
- *
- * WhichMovieWindow(w)
- * Checks if window is for a movie
- * and returns a pointer to it
- * or 0 if it isn't a movie's window
- *
- ***************************************************/
- MovieInstance* WhichMovieWindow(w)
-
- WindowPtr w;
-
- {
- short movieCount;
-
- /* Loop through looking for a match */
- for (movieCount = 0;movieCount<maxMovies;movieCount++)
- if (w == movieList[movieCount].window)
- break;
-
- if (movieCount<maxMovies) /* Did we find one? */
- return(&movieList[movieCount]);
-
- return(0L); /* Didn't find one */
- }
-
- /**************************************************
- *
- * IsMyWindow(w):Boolean
- * Checks if w is a valid window and mine
- *
- ***************************************************/
- Boolean IsMyWindow(w)
-
- WindowPtr w;
-
- {
- return( (w != 0L) &&
- WhichMovieWindow(w) );
- }
-
-
- /**************************************************
- ***************************************************
- *
- * Event handling stuff
- *
- ****************************************************
- ***************************************************/
-
- /**************************************************
- *
- * HandleMouseDown (theEvent)
- *
- * Take care of mouseDown events.
- *
- ***************************************************/
- void HandleMouseDown(theEvent)
-
- EventRecord *theEvent;
-
- {
- WindowPtr theWindow;
- int windowCode = FindWindow (theEvent->where, &theWindow);
-
- switch (windowCode)
- {
- case inSysWindow:
- SystemClick (theEvent, theWindow);
- break;
-
- case inMenuBar:
- AdjustMenus();
- HandleMenu(MenuSelect(theEvent->where));
- break;
-
- case inDrag:
- if (IsMyWindow(theWindow))
- SelectWindow(theWindow);
- DragWindow(theWindow, theEvent->where, &dragRect);
- break;
-
- case inContent:
- if (IsMyWindow(theWindow))
- {
- if (theWindow != FrontWindow())
- SelectWindow(theWindow);
- else
- MovieMouseDown(theWindow,theEvent->where,theEvent->modifiers); /* Track the mouse */
- }
- break;
-
- case inGoAway:
- if (IsMyWindow(theWindow) && TrackGoAway(theWindow, theEvent->where))
- CloseEm(theWindow); /* Close the appropriate windows */
- break;
-
- break;
- }
- }
-
-
- /**************************************************
- *
- * HandleEvent()
- *
- * The main event dispatcher. This routine should be called
- * repeatedly (it handles only one event).
- *
- ***************************************************/
- void HandleEvent()
-
- {
- int ok;
- EventRecord theEvent;
- WindowPtr theWindow;
-
- MovieInstance *theMovie;
-
- HiliteMenu(0);
- SystemTask (); /* Handle desk accessories */
-
- ok = GetNextEvent (everyEvent, &theEvent);
- /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- !
- ! Don't miss this!
- ! MoviesTask is called from the main event loop to
- ! give extra time to the active movie
- ! this is optional since CheckMovieControllers will update
- ! all of the movies. It also doesn't work too well yet.
- !
- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
- if(theEvent.what == nullEvent)
- MyMoviesTask();
-
- /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- !
- ! CheckMovieControllers is called from the main event loop to
- ! see if the event is for the player
- !
- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
- theModifiers = theEvent.modifiers; /* We may need these in the filter */
- if (CheckMovieControllers(&theEvent))
- return;
-
- else if (ok)
- switch (theEvent.what)
- {
- case mouseDown:
- HandleMouseDown(&theEvent);
- break;
-
- case keyDown:
- case autoKey:
- if ((theEvent.modifiers & cmdKey) != 0)
- {
- AdjustMenus();
- HandleMenu(MenuKey((char) (theEvent.message & charCodeMask)));
- }
- break;
-
- case updateEvt:
- /* To update the window, make the player replay the last frame */
- theWindow = (WindowPtr) theEvent.message; /* Get the window */
-
- theMovie = WhichMovieWindow(theWindow); /* Is it a Movie window? */
-
- BeginUpdate(theWindow);
- if(theMovie)
- DoMovieUpdate(theMovie);
- DrawControls(theWindow);
- EndUpdate(theWindow);
- break;
-
- case activateEvt:
- theMovie = WhichMovieWindow((WindowPtr) theEvent.message); /* Is it a Movie window? */
- if (theMovie)
- {
- if (theEvent.modifiers & activeFlag)
- DoMovieActivate(theMovie);
- else
- DoMovieDeactivate(theMovie);
- }
- else /* Some other window, make it redraw */
- {
- SetPort( (WindowPtr) theEvent.message );
- InvalRect(&((WindowPtr)theEvent.message)->portRect); /* Force an update event */
- }
-
- break;
- }
- }
-
- /**************************************************
- *
- * main()
- *
- * This is where everything happens
- *
- ***************************************************/
- main()
-
- {
- InitMacintosh(); /* Initialize everything */
- SetUpMenus();
- SetUpWindows();
- SetUpMovies();
-
- for (;;)
- HandleEvent();
- }
-